package com.mer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mer.dao.RoleDao;
import com.mer.dao.UserDao;
import com.mer.dao.UserRoleDao;
import com.mer.entity.Role;
import com.mer.entity.User;
import com.mer.entity.UserRole;
import com.mer.service.UserService;
import com.mer.vo.CommonResult;
import com.mer.vo.RoleVo;
import com.mer.vo.UserRoleVo;
import com.mer.vo.UserVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: IIPhase-project
 * @description:
 * @author: 冯彦凯
 * @create: 2021-10-20 19:18
 **/
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;
    @Resource
    private UserRoleDao userRoleDao;
    @Resource
    private RoleDao roleDao;
    @Override
    public CommonResult getAllUserByPage(Integer page, Integer limit, UserVo userVo) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(userVo.getUsername())){
            wrapper.like("username",userVo.getUsername());
        }
        if (userVo.getStartDate()!=null){
            wrapper.ge("gmt_create",userVo.getStartDate());
        }
        if (userVo.getEndDate()!=null){
            wrapper.le("gmt_create",userVo.getEndDate());
        }
        Page<User> page1 = new Page<>(page,limit);
        Page<User> userPage = userDao.selectPage(page1, wrapper);
        return new CommonResult(2000,"查询成功",userPage);
    }

    @Override
    public CommonResult addUserByUser(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        wrapper.eq("username",user.getUsername());
        if (userDao.selectOne(wrapper)!=null){
            return new CommonResult(5000,"该用户已经存在",null);
        }
        user.setPassword("2a7e188fb7243643d7ecea6667b2b7ff");
        String salt= "aaaaaa";
        user.setSalt(salt);
        userDao.insert(user);
        return new CommonResult(2000,"添加成功",null);
    }

    @Override
    public CommonResult updateUserByUser(User user) {
        userDao.updateById(user);
        return new CommonResult(2000,"修改成功",null);
    }

    @Override
    public CommonResult updateUserStatusById(String id, Integer deleted) {
        QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",id);
        List<UserRole> users = userRoleDao.selectList(wrapper);
        for (UserRole userRole : users) {
            if (userRole.getRoleId().equals("1")){
                return new CommonResult(5000,"超级管理员不能修改",null);
            }
        }
        User user = new User();
        user.setId(id);
        user.setIsDeleted(deleted);
        userDao.updateById(user);
        return new CommonResult(2000,"修改成功",null);
    }

    @Override
    public CommonResult deleteUserById(String id) {
        //记得userRole数据
        userDao.deleteById(id);
        return new CommonResult(2000,"删除角色成功",null);
    }

    @Override
    public CommonResult findRoleByUserId(String userId) {
        HashMap<String, Object> hashMap = new HashMap<>();
        QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
        List<Role> roles = roleDao.selectList(null);
        wrapper.eq("user_id",userId);
        List<UserRole> userRoles = userRoleDao.selectList(wrapper);
        List<String> collect = userRoles.stream().map(i -> i.getRoleId()).collect(Collectors.toList());
        hashMap.put("allRole",roles);
        hashMap.put("userRoleIds",collect);
        return new CommonResult(2000,"查询成功",hashMap);
    }

    @Override
    public CommonResult findRoleByRoleName(Integer page, Integer limit, RoleVo roleVo) {
        Page<Role> rolePage = new Page<>(page,limit);
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(roleVo.getRoleName())){
            wrapper.like("role_name",roleVo.getRoleName());
            Page<Role> roleData = roleDao.selectPage(rolePage, wrapper);
            return new CommonResult(2000,"查询成功",roleData);
        }else {
            Page<Role> roleData2 = roleDao.selectPage(rolePage, null);
            return new CommonResult(2000,"查询成功",roleData2);
        }
    }

    @Override
    public CommonResult giveRoleToUser(UserRoleVo userRoleVo) {
        QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userRoleVo.getUserId());
        userRoleDao.delete(wrapper);
        UserRole userRole = new UserRole();
        for (String roleId:userRoleVo.getRoleIds()){
            /*userRole.setGmtCreate(LocalDateTime.now());
            userRole.setGmtModified(LocalDateTime.now());*/
            userRole.setIsDeleted(0);
            userRole.setRoleId(roleId);
            userRole.setUserId(userRoleVo.getUserId());
            userRoleDao.insert(userRole);
        }
        return new CommonResult(2000,"操作成功",null);
    }


}
